﻿/*
 * Copyright (c) 2011 Roberto García López
 * 
 * email: vcrobe@gmail.com
 * 
 * website: http://vcrobe.blogspot.com
 * 
 * project host: http://vectorGraphics.codeplex.com
 * 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SlimDX;

namespace Ellipse
{
	/// <summary>
	/// Splines creations
	/// </summary>
	public static class Splines
	{

		#region Ellipse

		/// <summary>
		/// Creates an ellipse
		/// </summary>
		/// <param name="radiusX"></param>
		/// <param name="radiusY"></param>
		/// <param name="steps"></param>
		/// <returns></returns>
		public static PolygonData CreateEllipse(float radiusX, float radiusY, byte steps, bool generateMapCoords)
		{
			PolygonData pg = new PolygonData();

			byte st;
			int i;

			pg.vertices = new List<Vector3>();

			pg.vertices.Add(new Vector3(radiusX, 0, 0));
			//
			if (steps == 0)
			{
				pg.vertices.Add(new Vector3(0, radiusY, 0));
				pg.vertices.Add(new Vector3(-radiusX, 0, 0));
				pg.vertices.Add(new Vector3(0, -radiusY, 0));
			}
			else
			{
				double angleStep = 90 / (double)(steps + 1);
				double heading = angleStep, headingRad;

				// Calculate the vertices for the first quadrant
				for (st = 0; st < steps; st++, heading += angleStep)
				{
					headingRad = heading * Geometry.deg2Rad;
					pg.vertices.Add(new Vector3((float)(Math.Cos(headingRad) * radiusX), (float)(Math.Sin(headingRad) * radiusY), 0));
				}
				// Add the first vertex of the second quadrant
				pg.vertices.Add(new Vector3(0, radiusY, 0));
				// Calculate the vertices for the second quadrant
				for (i = pg.vertices.Count - 2, st = 0; st < steps; i--, st++)
				{
					pg.vertices.Add(new Vector3(-pg.vertices[i].X, pg.vertices[i].Y, 0));
				}
				// Add the first vertex of the third quadrant
				pg.vertices.Add(new Vector3(-radiusX, 0, 0));
				// Calculate the vertices for the second quadrant
				for (i = pg.vertices.Count - 2, st = 0; st < steps; i--, st++)
				{
					pg.vertices.Add(new Vector3(pg.vertices[i].X, -pg.vertices[i].Y, 0));
				}
				// Add the first vertex of the fourth quadrant
				pg.vertices.Add(new Vector3(0, -radiusY, 0));
				// Calculate the vertices for the second quadrant
				for (i = pg.vertices.Count - 2, st = 0; st < steps; i--, st++)
				{
					pg.vertices.Add(new Vector3(-pg.vertices[i].X, pg.vertices[i].Y, 0));
				}
			}
			if (generateMapCoords)
				pg.textureCoordinates = CreateTextureCoordinates(pg.vertices);
			return pg;
		}

		#endregion

		#region Helper methods

		/// <summary>
		/// Creates the texture coordinates for each vertex using the plannar mapping
		/// </summary>
		/// <param name="vertices"></param>
		/// <returns></returns>
		public static List<Vector2> CreateTextureCoordinates(List<Vector3> vertices)
		{
			List<Vector2> tc;
			ObjectBoundingRectangle bb = new ObjectBoundingRectangle(vertices);
			Vector2 len = bb.max - bb.min;

			tc = new List<Vector2>(vertices.Count);

			for (int i = 0; i < vertices.Count; i++)
				tc.Add(new Vector2((float)((vertices[i].X - bb.min.X) / len.X), (float)((bb.max.Y - vertices[i].Y) / len.Y)));
			return tc;
		}

		#endregion

	}
}
